<?php
class Ecore_Plugins_Controller extends Zend_Controller_Plugin_Abstract
{
	public function routeStartup(Zend_Controller_Request_Abstract $objRequest)
	{
	}

	public function routeShutdown(Zend_Controller_Request_Abstract $objRequest)
	{
	}

	public function dispatchLoopStartup(Zend_Controller_Request_Abstract $objRequest)
	{
	}

	public function preDispatch(Zend_Controller_Request_Abstract $objRequest)
	{
		$objView		= Zend_Registry::get('objView');
		$strController 	= $objRequest->getControllerName();
		$strAction 		= $objRequest->getActionName();
		$AppUI 			= Ecore_Session::start();
		Zend_Registry::set('AppUI', $AppUI);
		$objView->assign('AppUI', $AppUI);
        $arrActionNoRedirect = array (
                                        //index
                            			'facebook',
										'e404',
										//share
										'create',
										'dosave',
										'dopin',
										'doedit',
										'dodelete',
										'rss',
										//profile
										'ubdelete',
										'doachieve'
                                        
                               );	
		if ($strController == 'statics' ||
			$strController == 'ajax' || 
			$strController == 'valid' ||
			$strController == 'comment' ||
			$strController == 'frame' ||
			$strController == 'user'
			) {					
			return;
		}
        if (in_array($strAction, $arrActionNoRedirect)) return;        
		
		/** Set Back URL **/
		$strBackURL = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		if (!in_array(strrchr($strBackURL, '.'), array('.gif','.png','.jpg','.xml','.ico','.htc','.swf','.css'))) {
			if (strrchr($strBackURL, '.') == '.htc') exit;			
			$strBackURL = str_replace(array('?___=1','___=1','&___=1'),array('','',''),$strBackURL);
			$_SESSION[SESSION_BACKURL] = $strBackURL;
		}	
		if (isset($_COOKIE[COOKIE_AUTH]) && $_COOKIE[COOKIE_AUTH] != '' && $AppUI->id == 0) {	
			$arrCookieAuth 	= explode('::', Ecore_Function::base64D($_COOKIE[COOKIE_AUTH]));
			$strEmail 		= $arrCookieAuth[0];
			$isRemember		= isset($arrCookieAuth[2]) ? $arrCookieAuth[2] : 0;
			$objEcoreUser = new Ecore_User();
			$objUser		= $objEcoreUser->getUserByEmail($strEmail,$strDbPassword,$intStatus);		
			if($isRemember && $strChapPassword == $strPassword && $intStatus == 1) {		
				$objAuth = new Ecore_Auth();
				$AppUI = $objAuth->auth($objUser);
				Zend_Registry::set('AppUI', $AppUI);
				$objView->assign('AppUI', $AppUI);					
				Ecore_Function::redirect((isset($_SESSION[SESSION_BACKURL]) && $_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_FRONTEND);					
			}
		}
	}

	public function postDispatch(Zend_Controller_Request_Abstract $objRequest)
	{
	}

	public function dispatchLoopShutdown()
	{
	}
}
?>